package com.jy.api.service.impl;

import com.jy.api.dao.DiscountPromotionDao;
import com.jy.api.dao.DiscountPromotionProductDao;
import com.jy.api.domain.DiscountPromotion;
import com.jy.api.domain.DiscountPromotionProduct;
import com.jy.api.domain.PromotionType;
import com.jy.api.domain.TimeType;
import com.jy.api.service.DiscountPromotionService;
import com.jy.api.util.JodaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class DiscountPromotionServiceImpl implements DiscountPromotionService {

    @Autowired
    private DiscountPromotionDao discountPromotionDao;
    @Autowired
    private DiscountPromotionProductDao discountPromotionProductDao;


    @Override
    public Page<DiscountPromotion> findListByPage(String storeId, PromotionType type, int page, int size)throws Exception {
        List<PromotionType> types = new ArrayList<>();
        types.add(PromotionType.no);
        types.add(PromotionType.ing);
        List<DiscountPromotion> promotions = discountPromotionDao.findAllByStoreIdAndPromotionTypeIn(storeId, types);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        for (DiscountPromotion promotion : promotions) {
            switch (promotion.getPromotionType()){
                case no:
                    if (promotion.getTimeType().equals(TimeType.custom)){
                           if (sdf.parse(promotion.getBeginTime()).getTime() < new Date().getTime() && sdf.parse(promotion.getEndTime()).getTime() > new Date().getTime()){
                               discountPromotionProductDao.updateUnfinishedOrder(1,promotion.getId());
                               promotion.setPromotionType(PromotionType.ing);
                       }
                    }
                    break;
                case ing:
                    if (promotion.getTimeType().equals(TimeType.custom) && sdf.parse(promotion.getEndTime()+" "+promotion.getEndHour()).getTime() < new Date().getTime()){
                        discountPromotionProductDao.updateUnfinishedOrder(2,promotion.getId());
                        promotion.setPromotionType(PromotionType.over);
                    }
                    break;
            }
        }

        discountPromotionDao.save(promotions);
        Specification<DiscountPromotion> spec = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("promotionType"), type));
            predicates.add(criteriaBuilder.equal(root.get("storeId"), storeId));
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Page<DiscountPromotion> discountPromotions = discountPromotionDao.findAll(spec, new PageRequest(page, size, new Sort(Sort.Direction.DESC, "id")));
        return discountPromotions;
    }
}
